/* eslint-disable no-undef */


import { mapGetters } from 'vuex'
import Request from "@/api/baseRequest.js";

export const mixin_Coupon = {
    data() {
        return {
            urlParam: null,
            couponList: [],
            cardList: [],
            loginData: {
                account: null,
                password: null
            },
            write_off: {
                code: null,
                storeId: null
            }
        }
    },
    computed: {
        ...mapGetters([ // 使用辅助函数 mapGetters
            'getActivity', // 映射 this.perStatus为 this.$store.getters.perStatus
        ])
    },
    methods: {
        info(title) {
            this.$message(title);
        },
        success(title) {
            this.$message({
                message: title,
                type: 'success'
            });
        },
        warning(title) {
            this.$message.warning(title);
            this.$message({
                message: title,
                type: 'warning'
            });
        },
        error(title) {
            this.$message.error(title);
        },
        // 回退上一级
        back() {
            if (window.history.length <= 1) {
                this.$router.push({ path: "/" });
                return false;
            } else {
                this.$router.go(-1);
            }
            //上面都没执行就说明卡在当前页不是最后一条， histroy记录数量大于1，又没有回退记录，只能返回首页，
            //如果上面都执行了 页面都跳走了，这个也就不用管了
            setTimeout(() => {
                this.$router.push({ path: "/" });
            }, 500);
        },
        // 截取code
        GetParam(url, code) {
            url = url + "";
            let regstr = "/(\\?|\\&)" + code + "=([^\\&]+)/";
            let reg = eval(regstr);
            let result = url.match(reg);
            if (result && result[2]) {
                return result[2];
            }
        },
        async handleCoupon() {
            let params = {
                activityId: this.getActivity.id || this.GetParam(window.location.href, "id")
            };
            //根据后端所需传参数
            let { code, couponList } = await Request.listCoupon(params);
            if ([200, "200", "OK", 0].includes(code)) {
                this.couponList = couponList;
            }
        },
        async receiveSubmit() {
            let params = {
                activityId: this.getActivity.id || this.GetParam(window.location.href, "id"),
                userId: this.getActivity.userId || this.GetParam(window.location.href, "userId")
            }
            const { code } = await Request.takeCard(params);
            if ([200, "200", "OK", 0].includes(code)) {
                setTimeout(() => {
                    this.$router.push({ path: "/card" });
                }, 1500);
            }
        },
        async handleCard() {
            let params = {
                activityId: this.getActivity.id || this.GetParam(window.location.href, "id"),
                userId: this.getActivity.userId || this.GetParam(window.location.href, "userId")
            }
            const { code, cardList } = await Request.listCard(params);
            if ([200, "200", "OK", 0].includes(code)) {
                this.cardList = cardList;
            }
        },
        async loginDoorStore() {
            let params = this.loginData;
            const { code, storeId } = await Request.storeLogin(params);
            if ([200, "200", "OK", 0].includes(code)) {
                setTimeout(() => {
                    this.$router.push({ path: "/writeoff?storeId=" + storeId });
                }, 1500);
            }
        },
        async writeoffStore() {
            let self = this;
            this.$set(this.write_off, "storeId", (this.getActivity.storeId || this.GetParam(window.location.href, "storeId")))
            let params = this.write_off;
            const { code, msg } = await Request.verificationCouponCard(params);
            if ([200, "200", "OK", 0].includes(code)) {
                self.success(msg)
            } else {
                self.error(msg)
            }
        },
    },
    watch: {
        '$route.path': function(o) {
            this.urlParam = o;
        }
    },
    destroyed() {},
}
